//
//  ICObjects.m
//  Charles
//
//  Created by Charles on 2017/6/30.
//  Copyright © 2017年 charles. All rights reserved.
//

#import "IteratorProtocal.h"
#import "IteratorObjects.h"

@implementation MenuItem

- (instancetype)initWithName:(NSString *)name desc:(NSString *)desc vegetarian:(BOOL)vegetarian price:(double)price {
    if (self = [super init]) {
        self.name = name;
        self.desc = desc;
        self.vegetarian = vegetarian;
        self.price = price;
    }
    return self;
}

@end

@interface PancakeHouseMenu ()

@property (nonatomic,strong) NSMutableArray *menuItems;

@end

@implementation PancakeHouseMenu

- (instancetype)init {
    if (self = [super init]) {
        self.menuItems = [[NSMutableArray alloc]initWithCapacity:0];
        [self addItem:@"K&B's Pancake Breakfast" desc:@"Pancakes with scrabled eggs, and toast" vegetarian:NO price:2.99];
        [self addItem:@"Regular Pancake Breakfast" desc:@"Pancakes with fried eggs, sausage" vegetarian:NO price:2.99];
        [self addItem:@"Blueberry Pancake" desc:@"Pancakes made with fresh blueberries" vegetarian:NO price:2.99];
        [self addItem:@"Waffles" desc:@"Waffles, with your choice of blueberries or strawberries" vegetarian:NO price:2.99];
    }
    return self;
}

- (void)addItem:(NSString *)name desc:(NSString *)desc vegetarian:(BOOL)vegetarian price:(double)price {
    MenuItem *menuItem = [[MenuItem alloc]initWithName:name desc:desc vegetarian:vegetarian price:price];
    [self.menuItems addObject:menuItem];
}

//- (NSArray<MenuItem *> *)getMenuItems {
//    return _menuItems;
//}

- (id<Iterator>)createIterator {
    return [[PancakeHouseMenuIterator alloc]initWithMenuItems:self.menuItems];
}

@end

@interface PancakeHouseMenuIterator ()

@property (strong,nonatomic) NSMutableArray<MenuItem *> * items;

@end

@implementation PancakeHouseMenuIterator {
    NSInteger position;
}

-(instancetype)initWithMenuItems:(NSMutableArray<MenuItem *> *)menuItems {
    if (self = [super init]) {
        self.items = menuItems;
    }
    return self;
}

- (BOOL)hasNext {
    if (position >=  self.items.count ||  self.items[position] == nil) {
        return NO;
    }else {
        return YES;
    }
}

- (id)next {
    MenuItem *menuItem =  self.items[position];
    position++;
    return menuItem;
}

@end


static const NSInteger MAX_ITEMS = 6;

@interface DinerMenu ()

@property (strong,nonatomic) NSMutableArray<MenuItem *> *menuItems;
@property (assign,nonatomic) NSInteger numberOfItems;

@end

@implementation DinerMenu

- (instancetype)init {
    if (self = [super init]) {
        self.menuItems = [[NSMutableArray alloc]initWithCapacity:MAX_ITEMS];
        
        [self addItem:@"Vegetarian BLT" desc:@"(Fakin‘) Bacon with lettuce & tomator on whole wheat" vegetarian:YES price:2.99];
        [self addItem:@"BLT" desc:@"Bacon with lettuce & tomator on whole wheat" vegetarian:NO price:2.99];
        [self addItem:@"Soup of the day" desc:@"Soup of the day, with a side of potato salad" vegetarian:NO price:3.29];
        [self addItem:@"Hotdog" desc:@"A hot dog, with saurkrant,relish,onions,topped with cheese" vegetarian:NO price:3.05];
    }
    return self;
}

- (void)addItem:(NSString *)name desc:(NSString *)desc vegetarian:(BOOL)vegetarian price:(double)price {
    MenuItem *menuItem = [[MenuItem alloc]initWithName:name desc:desc vegetarian:vegetarian price:price];
    if (self.numberOfItems >= MAX_ITEMS) {
        NSLog(@"Sorry, menu is full ! Can not add item to menu");
    }else {
        self.menuItems[self.numberOfItems] = menuItem;
        self.numberOfItems++;
    }
}

/*
- (NSArray<MenuItem *> *)getMenuItems {
    return self.menuItems;
}*/

- (id<Iterator>)createIterator {
    
    return [[DinerMenuIterator alloc]initWithMenuItems:self.menuItems];
}

@end

@interface DinerMenuIterator ()

@property (strong,nonatomic) NSMutableArray<MenuItem *> * items;

@end

@implementation DinerMenuIterator {
    NSInteger position;
}

-(instancetype)initWithMenuItems:(NSMutableArray<MenuItem *> *)menuItems {
    if (self = [super init]) {
        self.items = menuItems;
    }
    return self;
}

- (BOOL)hasNext {
    if (position >=  self.items.count ||  self.items[position] == nil) {
        return NO;
    }else {
        return YES;
    }
}

- (id)next {
    MenuItem *menuItem =  self.items[position];
    position++;
    return menuItem;
}

@end

@interface CafeMenu ()

@property (strong,nonatomic) NSMutableArray *menuItems;

@end

@implementation CafeMenu

- (instancetype)init {
    if (self = [super init]) {
        self.menuItems = [[NSMutableArray alloc]initWithCapacity:0];
        [self addItem:@"Veggie Burger and Air Fries" desc:@"(Fakin‘) Bacon with lettuce & tomator on whole wheat" vegetarian:YES price:2.99];
        [self addItem:@"BLT" desc:@"Bacon with lettuce & tomator on whole wheat" vegetarian:NO price:2.99];
        [self addItem:@"Soup of the day" desc:@"Soup of the day, with a side of potato salad" vegetarian:NO price:3.29];
        [self addItem:@"Hotdog" desc:@"A hot dog, with saurkrant,relish,onions,topped with cheese" vegetarian:NO price:3.05];
    }
    return self;
}

- (void)addItem:(NSString *)name desc:(NSString *)desc vegetarian:(BOOL)vegetarian price:(double)price {
    MenuItem *menuItem = [[MenuItem alloc]initWithName:name desc:desc vegetarian:vegetarian price:price];
    [self.menuItems addObject:menuItem];
}

- (id<Iterator>)createIterator {
    return [[CafeMenuIterator alloc]initWithMenuItems:self.menuItems];
}

@end


@interface CafeMenuIterator ()

@property (strong,nonatomic) NSMutableArray<MenuItem *> * items;

@end

@implementation CafeMenuIterator {
    NSInteger position;
}

-(instancetype)initWithMenuItems:(NSMutableArray<MenuItem *> *)menuItems {
    if (self = [super init]) {
        self.items = menuItems;
    }
    return self;
}

- (BOOL)hasNext {
    if (position >=  self.items.count ||  self.items[position] == nil) {
        return NO;
    }else {
        return YES;
    }
}

- (id)next {
    MenuItem *menuItem =  self.items[position];
    position++;
    return menuItem;
}

@end

@interface Waitress ()

@property (strong,nonatomic) id<Menu> dinerMenu;
@property (strong,nonatomic) id<Menu> pancakeHoseMenu;
@property (strong,nonatomic) id<Menu> cafeMenu;

@end

@implementation Waitress

- (instancetype)initWithHouseMenu:(id<Menu>)houseMenu dinerMenu:(id<Menu>)dinerMenu cafeMenu:(id<Menu>)cafeMenu {
    if (self = [super init]) {
        self.pancakeHoseMenu = houseMenu;
        self.dinerMenu = dinerMenu;
        self.cafeMenu = cafeMenu;
    }
    return self;
}

- (void)printMenu {
    id<Iterator> pancakeIterator = [self.pancakeHoseMenu createIterator];
    id<Iterator> dinerIterator = [self.dinerMenu createIterator];
    id<Iterator> cafeIterator = [self.cafeMenu createIterator];
    NSLog(@"MENU\n----\nBREAKFAST");
    [self printMenu:pancakeIterator];
    NSLog(@"\nLUNCH");
    [self printMenu:dinerIterator];
    NSLog(@"\nDINNER");
    [self printMenu:cafeIterator];
}

- (void)printMenu:(id<Iterator>)iterator {
    while ([iterator hasNext]) {
        MenuItem *menuItem = (MenuItem *)[iterator next];
        NSLog(@"%@ %f %@",menuItem.name,menuItem.price,menuItem.desc);
    }
}

@end

@interface Waitress2 ()

@property (copy,nonatomic) NSArray *menus;

@end

@implementation Waitress2

- (instancetype)initWithMenus:(NSArray *)menus {
    if (self = [super init]) {
        self.menus = menus;
    }
    return self;
}

- (void)printMenu {
    for (id<Menu> menu in self.menus) {
        id<Iterator> iterator  = [menu createIterator];
        [self printMenu:iterator];
        NSLog(@"\n");
    }
}

- (void)printMenu:(id<Iterator>)iterator {
    while ([iterator hasNext]) {
        MenuItem *menuItem = (MenuItem *)[iterator next];
        NSLog(@"%@ %f %@",menuItem.name,menuItem.price,menuItem.desc);
    }
}

@end

